package cn.linter.oasys.user.service.impl;

import cn.linter.oasys.user.dao.FlowDao;
import cn.linter.oasys.user.dao.UserDao;
import cn.linter.oasys.user.entity.Flow;
import cn.linter.oasys.user.feign.FeignClientService;
import cn.linter.oasys.user.service.FlowService;
import cn.linter.oasys.user.vo.OperateVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.linter.oasys.user.vo.OAvo;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @ClassName: FlowServiceImpl
 * @Description:
 * @author: WangZe
 * @date: 2022/4/12 13:51
 */
@Service
@Slf4j
public class FlowServiceImpl implements FlowService {

    @Autowired
    private FeignClientService feignClientService;

    @Autowired
    private UserDao userDao;

    @Autowired
    private FlowDao flowDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public OAvo sendOA(String sendName, String applyName) {
        //检查用户是否存在
        if (userDao.select(sendName) == null) {
            throw new RuntimeException("发送人不存在");
        }else if (userDao.select(applyName) == null) {
            throw new RuntimeException("审批人不存在");
        }else{
            log.info("发送人："+sendName+"审批人："+applyName);
        }
        OAvo oAvo = new OAvo();
        //1、发起流程
        String oa = feignClientService.startLeaveProcess(sendName, applyName);
        log.info("OpenFeign请求结果： "+oa);
        JSONObject json = JSON.parseObject(oa);
        String processId = json.getString("processId");
        String taskId = json.getString("taskId");
        //2.保存相关数据到数据库
        Flow flow = new Flow();
        flow.setProcessId(processId);
        flow.setTaskId(taskId);
        flow.setSendName(sendName);
        flow.setApplyName(applyName);
        flow.setStatus("审批中");
        flowDao.save(flow);
        //3.返回结果
        oAvo.setTaskId(taskId);
        oAvo.setProcessId(processId);
        return oAvo;
    }

    /**
     * 审批
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public OperateVO operateOA(String taskId, String applyName, String result, String text){
        if(!flowDao.selectByTaskId(taskId).get(0).getStatus().equals("审批中")){
            throw new RuntimeException("该任务已经审批过了");
        }
        //0、检查审批人是否正确
        String man = flowDao.selectByTaskId(taskId).get(0).getApplyName();
        if (!man.equals(applyName)) {
            throw new RuntimeException("审批人不正确");
        }
        //1.审批
        try{
            String s = feignClientService.applyTask(taskId, result);
            log.info(s);
        }catch (Exception e){
            throw new RuntimeException("审批出现异常"+e.getMessage());
        }
        //2.更新数据库
        Flow flow = flowDao.selectByTaskId(taskId).get(0);
        flow.setStatus(result);
        flow.setText(text);
        flowDao.update(flow);
        log.info("更新数据库成功: "+result);
        //3.返回结果
        OperateVO operateVO = new OperateVO();
        operateVO.setOperateName(applyName);
        operateVO.setOaStatus(result);
        operateVO.setText(text);
        return operateVO;
    }

    @Override
    public List<Flow> findBySendUser(String userName) {
        if(userDao.select(userName) == null){
            throw new RuntimeException("用户名不存在");
        }
        return flowDao.findBySendUser(userName);
    }

    @Override
    public List<Flow> findByApplyUser(String userName) {
        if(userDao.select(userName) == null){
            throw new RuntimeException("用户名不存在");
        }
        return flowDao.findByApplyUser(userName);
    }

    @Override
    public int delete(String taskId, String processId) {
        try {
            flowDao.delete(taskId, processId);
            return 0;
        }catch (Exception e){
            return -1;
        }
    }

}
